local penglai = fk.CreateSkill {
  name = "th_penglai",
  tags = { Skill.Compulsory },
}

penglai:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(penglai.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local draws = player:drawCards(1, penglai.name)
    local all_hand = player:getCardIds("h")
    local hands = table.simpleClone(all_hand)
    local x = 0
    for i = #hands, 1, -1 do
      if table.contains(draws, hands[i]) then
        table.remove(hands, i)
        x = x + 1
      end
    end
    if #hands < 1 or not next(draws) or x < #draws then return end
    local suits = {}
    for _, cid in ipairs(hands) do
      local card = Fk:getCardById(cid)
      if card then
        table.insertIfNeed(suits, card.suit)
      end
    end
    local suits_draw = table.map(draws, function(pid) return Fk:getCardById(pid).suit end)
    if table.find(suits, function(s) return table.contains(suits_draw, s) end) then
      local drawstr = table.concat(table.map(draws, function(pid) return Fk:getCardById(pid):toLogString() end), "、")
      if room:askToSkillInvoke(player, {
        skill_name = penglai.name,
        prompt = "th_penglai-discard:::" .. drawstr
      }) then
        room:throwCard(hands, penglai.name, player, player)
        if player.dead or player:getCardIds("h") == all_hand then return end
        room:recover{
          who = player,
          num = 1 - player.hp,
          skillName = penglai.name,
          recoverBy = player
        }
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["th_penglai"] = "蓬莱",
  [":th_penglai"] = "锁定技，当你进入濒死状态时，你摸一张牌，若其中一张牌的花色与其余任意手牌相同，你可以弃置其余所有手牌并回复至1点体力。",

  ["th_penglai-discard"] = "蓬莱：你可以弃置除 %arg 外的所有手牌并回复至1点体力",
}

return penglai